const cart = require('../../../sys/cart')

var behaviorsbasic = require('../../behavior/basic')
Component({
  behaviors: [behaviorsbasic],
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },

  properties: {
    objmodel: {
      type: Object,
      default: ''
    },
    datasource: {
      type: Object,
      default: ''
    },
    cartpro: {
      type: Object,
      default: ''
    },

  },


  ready() {
    var that=this
    


    this.setData({
      scrollleft: 1,
      syswidth: wx.getSystemInfoSync().windowWidth
    })

  },
  /**
   * 组件的初始数据
   */
  data: {
    cart: {},
    dotnums: 0,
    scrollleft: 0,
    syswidth: 375,
    scrollWidth: 0,
    platform:"" //手机类型
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initsiper(ev) {     
    
      var scrollWidth = ev.detail.scrollWidth
      var syswidth = this.data.syswidth
      var mod_val = scrollWidth % syswidth
      var dotnums = (scrollWidth - mod_val) / syswidth + (mod_val > 0 ? 1 : 0)      
      this.setData({ scrollleft: 0, scrollWidth, dotnums })
    },
    changescrollleft(ev) {
      switch (ev.op) {
        case "setleft": {
          var scrollleft = this.data.syswidth * ev.idx
          if (this.data.scrollleft == scrollleft) {
            scrollleft--
          }
          this.setData({ scrollleft: scrollleft })
          break;
        }
      }
    },
   
    noevent(ev) {
      //用来阻止用的， 其它没什么用处

    },

    cartop(ev) {


  



      var detail = ev.detail;
      var item = detail.item;
      switch (detail.op) {
        case "setnum": {
          var keyval = item.id + "_0_0_-1";
          cart.setnum(keyval, detail.num, true);
          break;
        }
        case "add": {

         
          if (item.guigetype == "-1") {
            item["num"] = 1
       
      
            cart.cartadd(item)
          }
          else {
            this.triggerEvent('buy', { dataset: detail })
          }
          break;

        }
      }
      this.triggerEvent('cartop', { op: "reload" })

    },
    buy(ev) {
      var dataset = ev.currentTarget;
      this.triggerEvent('buy', dataset)
    },

  }
})
